<?php
/**
 * Memcached 缓存操作 (php-memcached)
 *
 * @filesource phpmencache.class.php
 * @package peck
 * @subpackage _cache
 * @version $id: 0.1, utf8, 20100506
 * @author LD King <kldscs[at]gmail.com>
 * @copyright Copyleft (D.) 2007 - 2010 MiFunny China Inc.
 * @link http://mifunny.info/
 * @see DsCache.class.php
 *   http://pecl.php.net/package/memcache
 *   http://cn.php.net/manual/en/ref.memcache.php
 */
class _memcache{ 
        
	/**
	 * Memcached 连接句柄
	 *   A Memcached object. 
	 * @var object
	 */
	public $_m = null;

	 /**
     * 默认的缓存策略
     *
     * servers - 缓存服务器配置，参看$_default_server, 允许多个缓存服务器;
     * 			host - 缓存服务器地址或主机名;
     * 			port - 缓存服务器端口;
     * 			weight - 服务器权重, 越大越好;
     * 		array( 'host' => '127.0.0.1', 'port' => '11211', 'weight' => 99, ),
     * 
     * compressed - 是否压缩缓存数据;
     * life_time - 缓存有效时间, 如果设置为 0 表示缓存永不过期;
     * persistent - 是否使用持久连接;
     * encoding_key - 是否加密Key, 默认false;
     * 
     * @var array
     */
	protected $_default_policy = array(
        'servers' => array(),
        'compressed' => false,
        'life_time' => 900,
        'persistent' => true,
		'encoding_key' => false,
    );
	
    
    public function __construct(array $policy=null){
        if (extension_loaded('memcache')){
            if( !is_null($policy) ){
                $this->_init($policy);
            }
    	}else{
    		throw new LogicException("Error: Please Configure Mencache extension first!");
    	}
    }
    
    /**
     * 初始化 Memcached 对象
     * 
     * @param array $policy 配置数组
     */
    public function _init(array $policy=null){
        if ( !is_null($policy) ){
            $this->_default_policy = array_merge($this->_default_policy, $policy);
        }else{
            throw new RuntimeException("Error: where is your Memcached policy ?");
        }
        
        if(true === $this->_default_policy['compressed']){
        	$this->_default_policy['compressed'] = MEMCACHE_COMPRESSED;
        }
        
        $this->_m = new Memcache;
        $this->_default_policy['compressed'] = empty($this->_default_policy['compressed']) ? null : MEMCACHE_COMPRESSED;
		//是否压缩 $this->_m->setOption(Memcached::OPT_COMPRESSION, $this->_default_policy['compressed']);
		//长连接 $this->_m->setOption(Memcached::OPT_LIBKETAMA_COMPATIBLE, $this->_default_policy['persistent']);
		//服务器
		if( !empty($this->_default_policy['servers']) ){
			foreach($this->_default_policy['servers'] as $ss){
				if( !$this->_m->addServer($ss['host'], $ss['port'],
					 $this->_default_policy['persistent']) )
					throw new RuntimeException("Error: add cache servers failed, check your servers array.");
			}
			
		}else{
			throw new LogicException("Error: no cache server add.");
		}
    }

    //设置缓存
    public function set($id, $data, $expiration=null){
    	$life_time = is_null($expiration) ? $this->_default_policy['life_time'] : $expiration;
    	$key = $this->_key($id);
    	
    	return $this->_m->set($key, $data, 
    		$this->_default_policy['compressed'], $life_time);
    } 

	//获得缓存
    public function get($id, $expiration=null){
        $key = $this->_key($id);
        
        return $this->_m->get($key);
    }
	
    //清除特定缓存
    public function remove($id, $time=0){
		$key = $this->_key($id);
    	 
    	return $this->_m->delete($key, $time);
    }

	//清空缓存
    public function clean($delay=0){
        return $this->_m->flush($delay);
    }

    /**
     * 确定缓存文件名
     *
     * @param string $id
     * @param array $policy
     *
     * @return string
     */
    protected function _key($id){
    	
    	if ( $this->_default_policy['encoding_key'] ){
			$key = 'DsCache_' . md5( $id );
		}else{
			//$key = 'DsCache_' . $id;
			$key = $id;
		}	
        return $key;
    }

}//END class eaccelerator
